
let usernameReg = /^1[3-9][0-9]{9}$/
let pswReg = /^[a-z0-9_-]{6,20}$/
let enter_name = $('.enter_name')[0]
let enter_psw = $('.enter_psw')[0]
function login() {
    let username = $('.username')[0]
    let password = $('.password')[0]
    //用户名、密码正则验证功能
    username.onfocus = function () {
        enter_name.innerHTML = '请您确认填写您的手机号'
        enter_name.style.display = 'block'
    }
    username.onblur = function () {
        if (usernameReg.test(username.value) || username.value == '') {
            enter_name.style.display = 'none'
        } else {
            enter_name.innerHTML = '请输入正确的手机号！'
        }
    }
    password.onfocus = function () {
        enter_psw.innerHTML = '密码必须输入6-20位字符'
        enter_psw.style.display = 'block'
    }
    password.onblur = function () {
        if (pswReg.test(password.value) || password.value == '') {
            enter_psw.style.display = 'none'
        } else {
            enter_psw.innerHTML = '密码必须输入6-20位字符！'
        }
    }
    //登录功能
    let login = $('.login')[0]
    login.onclick = () => {
        let username = $('.username')[0].value
        let password = $('.password')[0].value
        let list = JSON.parse(localStorage.getItem('login')) || []
        if (list.length) {
            //获取到的是一个存放对象的数组，取第0个就是对象
            if (password == list[0].password && username == list[0].username) {
                alert('登录成功！')
                location.href = '/index.html'
                return
            } else {
                enter_psw.innerHTML = '手机号或密码错误'
                enter_psw.style.display = 'block'
                return
            }
        } else {
            enter_psw.innerHTML = '手机号或密码错误'
            enter_psw.style.display = 'block'
        }
    }
}
login()
